package com.ruoyi.web.controller.major;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.major.domain.StuMagor;
import com.ruoyi.major.service.IStuMagorService;
import com.ruoyi.stage.domain.StuStage;
import com.ruoyi.system.domain.SysPost;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/magor/magorlist")
public class StuMajorController extends BaseController {
    @Resource
    private IStuMagorService stuMagorService;

    @PreAuthorize("@ss.hasPermi('major:majorlist:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuMagor stuMagor)
    {
        startPage();
        List<StuMagor> list = stuMagorService.selectMagorList(stuMagor);
        return getDataTable(list);
    }


//    @GetMapping(value = { "/" })
//    public AjaxResult getInfo()
//    {
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("magors", stuMagorService.selectAllMagorList());
//        return ajax;
//    }

    /**
     * 根据id查询专业
     */
    @GetMapping(value = "/{magId}")
    @PreAuthorize("@ss.hasPermi('major:majorlist:list')")
    public AjaxResult getInfo(@PathVariable Long magId)
    {
        return AjaxResult.success(stuMagorService.selectMagorById(magId));
    }



    /**
     * 新增专业
     * @param stuMagor
     * @return
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('major:majorlist:add')")
    @Log(title = "部门管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated @RequestBody StuMagor stuMagor)
    {
        if (UserConstants.NOT_UNIQUE.equals(stuMagorService.checkMajorNameUnique(stuMagor)))
        {
            return AjaxResult.error("新增专业'" + stuMagor.getMagName() + "'失败，专业名称已存在");
        }
        stuMagor.setCreateBy(getUsername());
        return toAjax(stuMagorService.insertMagor(stuMagor));
    }

    /**
     * 修改专业
     * @param stuMagor
     * @return
     */
    @PutMapping
    @PreAuthorize("@ss.hasPermi('major:majorlist:edit')")
    @Log(title = "部门管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated @RequestBody StuMagor stuMagor)
    {
        if (UserConstants.NOT_UNIQUE.equals(stuMagorService.checkMajorNameUnique(stuMagor)))
        {
            return AjaxResult.error("修改岗位'" + stuMagor.getMagName() + "'失败，专业名称已存在");
        }
        stuMagor.setUpdateBy(getUsername());
        return toAjax(stuMagorService.updateMagor(stuMagor));
    }

    /**
     * 删除专业
     */
    @DeleteMapping("/{magIds}")
    @PreAuthorize("@ss.hasPermi('major:majorlist:remove')")
    @Log(title = "部门管理", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] magIds)
    {
        return toAjax(stuMagorService.deleteMagorByIds(magIds));
    }


    @Log(title = "专业管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:post:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, StuMagor stuMagor)
    {
        List<StuMagor> list = stuMagorService.selectMagorList(stuMagor);
        ExcelUtil<StuMagor> util = new ExcelUtil<StuMagor>(StuMagor.class);
        util.exportExcel(response, list, "专业数据");
    }


}
